package nlppatents.util;

import java.util.HashMap;

public class POSWord 
{
	public static HashMap<String, POSWord> words;
	public static final int INITIAL_WORDS_CAPACITY = 500000; 
	
	public static void init()
	{
		words = new HashMap<String, POSWord>(INITIAL_WORDS_CAPACITY);
	}
	
	public static POSWord get(String s)
	{
		// extract base part of word
		int sIndex = s.lastIndexOf("/");
		if(sIndex < 0)
		{
			System.err.println("ERROR:  \"" + s + "\" not a valid word.");
			return null; // not a valid word
		}
		// extract parts
		String word = s.substring(0, sIndex);
		String tag = s.substring(sIndex+1);
		
		if((word.length() == 0) || (tag.length() == 0))
		{
			System.err.println("ERROR: Word had 0 length.");
			return null;
		}
		// set up words hashString
		word = word.toLowerCase();
		tag = tag.toUpperCase();
		POS p = POS.get(tag);
		if(p == null)
		{
			//return null;  // <-- Bad behavior!  Unencountered POS's should be treated as WORD's
			p = POS.get("WORD");
			POS.put(tag, p);  // ensures that later lookups of unknown POS result in word
			System.err.println("New unknown POS encountered: \"" + tag + "\".  Classifying as a \"WORD\"");
		}
		String hashWord = word + "/" + p.label;
		
		// get or instantiate actual word
		POSWord w = words.get(hashWord);
		if(w == null)
		{
			w = new POSWord(word, p);
			words.put(w.hashString, w);
		}
		
		return w;
	}
	
	public String word;
	public String hashString;
	public POS pos;
	public DictWord dictWord;
	
	public POSWord(String word, POS pos)
	{
		this.word = word;
		this.pos = pos;
		this.hashString = word + "/" + pos.label;
		
		this.dictWord = DictWord.get(word);
		//this.dictWord.add(this);
	}
	
	public void incFreq()
	{
		dictWord.freq++;
	}
	public void incColloc()
	{
		dictWord.coloc++;
	}
	
	/*
	public boolean equals(POSWord w)
	{
		return this.dictWord == w.dictWord;
	}
	*/
}
